#include <pthread.h>
#include "head.h"
#include "operate.h"

#define OP_MUTEX op_mutex
static pthread_mutex_t op_mutex = PTHREAD_MUTEX_INITIALIZER;

/*根据指令类型调用不同的函数*/
int handleCli(Client *cli){
    switch(cli->cmd.type){
    case CMD_LS:        //ls指令
        printf("命令\"ls\"处理中...\n");
        handleArg(cli->user.path, cli->cmd.cmd_arg);
        op_ls(cli);
        break;      
    case CMD_CD:        //cd指令
        printf("命令\"cd\"处理中...\n");
        handleArg(cli->user.path, cli->cmd.cmd_arg);
        op_cd(cli);
        break;
    case CMD_PUTS:      //puts指令
        printf("命令\"puts\"处理中...\n");
        handleArg(cli->user.path, cli->cmd.cmd_arg);
        op_puts(cli);
        break;
    case CMD_GETS:      //gets指令
        printf("命令\"gets\"处理中...\n");
        handleArg(cli->user.path, cli->cmd.cmd_arg);
        op_gets(cli);
        break;
    case CMD_MKDIR:      //mkdir指令
        printf("命令\"mkdir\"处理中...\n");
        handleArg(cli->user.path, cli->cmd.cmd_arg);
        op_gets(cli);
        break;
    case CMD_RM:        //rm指令
        printf("命令\"rm\"处理中...\n");
        handleArg(cli->user.path, cli->cmd.cmd_arg);
        op_rm(cli);
        break;
    case CMD_PWD:       //pwd指令
        break;
    case CMD_LOGIN:     //login指令
        handleArg(cli->user.path, cli->cmd.cmd_arg);
        op_login(cli);
        break;
    case CMD_REGISTER:  //register指令
        handleArg(cli->user.path, cli->cmd.cmd_arg);
        op_register(cli);
        break;
    default:
        break;
    }
    return 0;
}

/*处理指令参数中的 /.. */
int handleArg(char *cli_path, char *arg){
    //todo
    char buf[OP_BUF_SIZE] = {0};
    strcat(buf, cli_path);
    strcat(buf, arg);

    strcpy(arg, buf);
    return 0;
}

///*ls指令，成功返回0，失败返回-1*/
//int op_ls(Client *cli){
//    //todo
//    char buf[OP_BUF_SIZE] = {0};
//    sprintf(buf, "ls %s", cli->cmd.cmd_arg);
//    
//    FILE *fp = popen(buf, "r");
//    memset(buf, 0, sizeof(buf));
//    
//    int ret = fread(buf,sizeof(char), sizeof(buf), fp);
//    if(ret == -1){
//        //logError
//        printf("ret == -1\n");
//        return -1;
//    }else if(ret == 0){
//        //logWarning
//        return -1;
//    }else{
//        memset(&cli->cmd, 0, sizeof(cli->cmd));
//        memset(&cli->file, 0, sizeof(cli->file));
//        cli->file.file_size = ret;
//        /*发送结构体*/
//        send(cli->user.fd_net, cli, sizeof(*cli), MSG_NOSIGNAL);
//        /*发送数据*/
//        send(cli->user.fd_net, buf, ret, MSG_NOSIGNAL);
//        fclose(fp);
//        return 0;
//    }    
//}
//
///*cd指令，成功返回0，失败返回-1*/
//int op_cd(Client *cli){
//    //todo
//    return 0;
//}
//
///*puts指令，成功返回0，失败返回-1*/
//int op_puts(Client *cli){
//    //todo
//    return 0;
//}
//
///*gets指令，成功返回0，失败返回-1*/
//int op_gets(Client *cli){
//    //todo
//    return 0;
//}
//
///*gets指令，成功返回0，失败返回-1*/
//int op_mkdir(Client *cli){
//    //todo
//    return 0;
//}
//
///*rm指令，成功返回0，失败返回-1*/
//int op_rm(Client *cli){
//    //todo
//    return 0;
//}
//
///*login指令，成功返回0，失败返回-1*/
//int op_login(Client *cli){
//    //todo
//    return 0;
//}
//
///*register指令，成功返回0，失败返回-1*/
//int op_register(Client *cli){
//    //todo
//    return 0;
//}
